/**Created by the LayaAirIDE*/
package view
{
	import ui.GamePauseUI
	import laya.events.Event;

	public class GamePause extends GamePauseUI
	{
		public function GamePause() 
		{
			this.on(Event.ADDED,this,onAddToStage);
			this.on(Event.REMOVED,this,onRemoveFromStage);
		}
		
		private function onAddToStage():void
		{
			onUpdataHP();
			onUpdataScore();
			btn_pause.on(Event.CLICK,this,onClickPause);
			GameDispatcher.getInstance().on(EventName.SCORE_CHANGE,this,onUpdataScore);
			GameDispatcher.getInstance().on(EventName.HP_CHANGE,this,onUpdataHP);
			
		}

		private function onRemoveFromStage():void
		{
			GameDispatcher.getInstance().off(EventName.SCORE_CHANGE,this,onUpdataScore);
			GameDispatcher.getInstance().off(EventName.HP_CHANGE,this,onUpdataHP);
			btn_pause.off(Event.CLICK,this,onClickPause);
			box_pause.visible = false;
			btn_pause.visible = true;
		}

		private function onClickPause():void
		{
			box_pause.visible = true;
			btn_pause.visible = false;
			this.once(Event.MOUSE_DOWN,this,onClickPage);
			GameDispatcher.getInstance().event(EventName.GAME_PAUSE);
		}

		private function onClickPage():void
		{
			box_pause.visible = false;
			btn_pause.visible = true;
			GameDispatcher.getInstance().event(EventName.GAME_RESUME);
		}

		private function onUpdataScore():void
		{
			txt_score.text = "SCORE:" + GameManager.getInstance().score.toString();
		}

		private function onUpdataHP():void
		{
			txt_hp.text = "HP:" + GameManager.getInstance().hero.hp.toString();
		}
	}

}